﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HandRise.MBHOP.Controls;
using Handrise.CDCable.Mobile.Business;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmFlawDeal :FrmBase
    {
        private Flaw flaw = null;
        private FlawService flawService = new FlawService();

        public FrmFlawDeal()
        {
            InitializeComponent();
            ttlbrhead.Text = ResourceImage.缺陷管理;
            ilstbcup.Images.Add(ResourceImage.验收01);
            ilstbcup.Images.Add(ResourceImage.驳回01);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.验收02);
            ilstdown.Images.Add(ResourceImage.驳回02);
            ilstdown.Images.Add(ResourceImage.返回02);

        }

        private void FrmFlawDeal_Load(object sender, EventArgs e)
        {
            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

            ccFlawDetail.AddMember("DeviceName", "设备名称");
            ccFlawDetail.AddMember("DeviceType", "设备类型");
            ccFlawDetail.AddMember("Part", "缺陷部位");
            ccFlawDetail.AddMember("Phenomenon", "缺陷现象");
            ccFlawDetail.AddMember("DealState", "缺陷状态");
            if(flaw.DealState == "退回")
                ccFlawDetail.AddMember("RejectReason", "驳回原因");
            ccFlawDetail.AddMember("Level", "缺陷级别");
            ccFlawDetail.AddMember("DisCoverer", "发现人");
            ccFlawDetail.AddMember("DisCoverDateTime", "发现时间");
            ccFlawDetail.DataSource = flaw;
            ccFlawDetail.DataBind();

            tbTitle.Text = flaw.Title;
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            flaw = (Flaw)Parameter[0];
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        private void LoadCacheData()
        {
            flaw = (Flaw)Caches[0];

        }

        private void bcOpereate_OnButtonClick(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 0: //验收
                    AcceptanceFlaw();
                    break;
                case 1:  //驳回
                    RejectFlaw();
                    break;
                case 2: //返回
                    FormManage.GotoNext("FrmFlawList", null, null, true);
                    break;
            }
        }

        private void AcceptanceFlaw()
        {
            if (flaw.ReportState == "正在上报" || flaw.ReportState == "已上报")
            {
                mssdlgConfirm.ShowDialog(this, "当前缺陷" + flaw.ReportState + "，不允许进行操作", DialogType.Confirm, FormManage.form_Deactivate);
            } 
            
            if (mssdlgexec.ShowDialog(this, "是否验收当前缺陷？", DialogType.Select, FormManage.form_Deactivate) == MessageDialogResult.Button2)
                return;

            if (flawService.AcceptanceFlaw(flaw.FlawID))
            {
                flaw.DealState = "已验收";
            }

            ccFlawDetail.DataSource = flaw;
            ccFlawDetail.DataBind();

            mssdlgConfirm.ShowDialog(this, "验收完成", DialogType.Confirm, FormManage.form_Deactivate);
        }

        private void RejectFlaw()
        {
            if (flaw.ReportState == "正在上报" || flaw.ReportState == "已上报")
            {
                mssdlgConfirm.ShowDialog(this, "当前缺陷" + flaw.ReportState + "，不允许进行操作", DialogType.Confirm, FormManage.form_Deactivate);
            }

            if (flaw.DealState != "退回")
            {
                if (mssdlgexec.ShowDialog(this, "确认驳回当前缺陷？", DialogType.Select, FormManage.form_Deactivate) == MessageDialogResult.Button2)
                    return;
            }

            if (flawService.RejectFlaw(flaw.FlawID))
            {
                flaw.DealState = "退回";
            }

            FormManage.GotoNext("FrmFlawReject", new object[]{flaw}, new object[]{flaw}, false);

        }

        private void btnPrior_Click(object sender, EventArgs e)
        {
            ccFlawDetail.PreviousPage();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            ccFlawDetail.NextPage();
        }



    }
}